# -*- coding: utf-8 -*- 
# 作者:飞虎
# 开发时间:2022/5/1 21:40
from PyQt5.Qt import *


class Window(QWidget):
	def __init__(self):
		self.move_flag = False
		super().__init__()
		self.setWindowTitle('窗口移动学习')
		self.resize(500, 500)
		self.setup_ui()

	def setup_ui(self):
		pass

	def mousePressEvent(self, evt):
		if evt.button() == Qt.LeftButton:
			self.move_flag = True
			# QMouseEvent
			# print('鼠标按下')
			# 确定两个点第一次按下的点,窗口当前所在的原始点
			self.mouse_x = evt.globalX()
			self.mouse_y = evt.globalY()
			# print(self.mouse_x, self.mouse_y)

			self.origin_x = self.x()
			self.origin_y = self.y()

	def mouseMoveEvent(self, evt):
		if self.move_flag:
			# print(evt.globalX(), evt.globalY())
			move_x = evt.globalX() - self.mouse_x
			move_y = evt.globalY() - self.mouse_y
			# print(move_x, move_y)
			dest_x = self.origin_x + move_x
			dest_y = self.origin_y + move_y
			self.move(dest_x, dest_y)

	def mouseReleaseEvent(self, QMouseEvent):
		self.move_flag = False


if __name__ == '__main__':
	import sys

	app = QApplication(sys.argv)
	window = Window()
	window.show()
	window.setMouseTracking(True)
	sys.exit(app.exec_())
